import { ListenEventType, TouchEventType } from "../util/defineConfig";
import ONEventCtrl from "../util/OnEventCtrl";
import OnEventMode from "../util/OnEventMode";
import audioManager from "./audioManager";
import { GameCtrl } from "./gameCtrl";
import GameModel from "./gameModel";
import GameView from "./gameView";
import nakama from "./nakama";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GameScene extends OnEventMode {
     @property(GameView)
     gameView: GameView = null
     @property(cc.Node)
     musicCtrl: cc.Node = null
     @property(audioManager)
     audioManager: audioManager = null

     private gameCtrl: GameCtrl = null
     private gameModel: GameModel = null
     private onEventCtrl: ONEventCtrl = null
     // private nakama: Nakama = null

     private isPauseMusic: boolean = false

     public static isMute: boolean = false
     onLoad() {
          this.gameCtrl = new GameCtrl()
          this.gameModel = new GameModel()

          this.onEventCtrl = new ONEventCtrl()
          // this.nakama = new Nakama()
          
          
          this.onEventCtrl.regesterModel(this.gameModel, this.gameView, this.gameCtrl, this.audioManager)
          this.onEventCtrl.onEventListen()

          this.gameCtrl.init(this.gameView, this.gameModel)
          this.musicCtrl.on(TouchEventType.touchStart, this.bgMusicCtrl, this)
     }

     start() {
          if (GameScene.isMute) {
               this.isPauseMusic = GameScene.isMute
               this.gameCtrl.isMute = this.isPauseMusic
               this.musicCtrl.children[0].active = !this.isPauseMusic
               this.musicCtrl.children[1].active = this.isPauseMusic

          }
     }

     //    背景音乐控制,防止重新开始游戏时关闭的声音又打开
     private bgMusicCtrl() {
          this.isPauseMusic = !this.isPauseMusic
          this.gameCtrl.isMute = this.isPauseMusic
          this.musicCtrl.children[0].active = !this.isPauseMusic
          this.musicCtrl.children[1].active = this.isPauseMusic
          if (this.isPauseMusic) {
               this.emit(ListenEventType.PAUSE_MUSIC)
          } else {
               this.emit(ListenEventType.PLAY_MUSIC)
          }
     }

     onDestroy() {
          this.onEventCtrl.offEventListen()
     }
}
